home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / dbmalloc / cctest.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  11KB  |  690 lines

  1. /*
  2.  * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  3.  *
  4.  * This software may be distributed freely as long as the following conditions
  5.  * are met:
  6.  *         * the distribution, or any derivative thereof, may not be
  7.  *          included as part of a commercial product
  8.  *        * full source code is provided including this copyright
  9.  *        * there is no charge for the software itself (there may be
  10.  *          a minimal charge for the copying or distribution effort)
  11.  *        * this copyright notice is not modified or removed from any
  12.  *          source file
  13.  */
  14. #if __STDC__ || __cplusplus
  15. # define __stdcargs(s) s
  16. #else
  17. # define __stdcargs(s) ()
  18. #endif
  19.  
  20. #ifdef USE_STDLIB
  21. #include <stdlib.h>
  22. #endif
  23. #ifdef USE_UNISTD
  24. #include <unistd.h>
  25. #endif
  26. #ifdef USE_MALLOC
  27. #include <malloc.h>
  28. #endif
  29. #ifdef USE_MEMORY_H
  30. #include <memory.h>
  31. #endif
  32. #ifdef USE_STRING_H
  33. #include <string.h>
  34. #endif
  35. #ifdef USE_SYSENT
  36. #include <sysent.h>
  37. #endif
  38.  
  39.  
  40. /*
  41.  * $Id: cctest.c,v 1.12 1992/08/22 16:27:13 cpcahil Exp $
  42.  */
  43. /*
  44.  * This file is not a real source file for the malloc library.  The
  45.  * configuration utility uses this file to test the various compiler
  46.  * settings that can be used by the library.
  47.  */
  48.  
  49. #ifdef VOIDTEST
  50.     /*
  51.      * testing to see if the void datatype is used by this system
  52.      */
  53.     void *
  54.     function()
  55.     {
  56.         static void * t;
  57.  
  58.         return(t);
  59.     }
  60. #endif
  61.  
  62. #ifdef EXITTEST
  63.  
  64. /*
  65.  * determine the return type of exit
  66.  */
  67. #if __cplusplus
  68.     extern "C" {
  69. #endif
  70.         EXITTYPE exit __stdcargs((int));
  71. #if __cplusplus
  72.     }
  73. #endif
  74. #if __cplusplus || __STDC__
  75. #include <stdio.h>
  76. main(int argc, char *argv[])
  77. #else
  78. main(argc,argv)
  79.     int      argc;
  80.     char    * argv[];
  81. #endif
  82. {
  83.  
  84.     /*
  85.      * this bogus stuff is here simply to get c++ to shut-up about
  86.       * unreferenced parameters.
  87.      */
  88.     if( argv[argc] == "hello" )
  89.     {
  90.         printf("hello\n");
  91.     }
  92.     return(0);
  93. }
  94. #endif /* EXITTEST */
  95.  
  96. #ifdef SETENVTEST
  97. /*
  98.  * determine if setenv is supported
  99.  */
  100. #if __cplusplus || __STDC__
  101. #include <stdio.h>
  102. main(int argc, char *argv[])
  103. #else
  104. main(argc,argv)
  105.     int      argc;
  106.     char    * argv[];
  107. #endif
  108. {
  109. #ifdef setenv
  110. #undef setenv
  111. #endif
  112.  
  113.     setenv("TESTSYM","YES",1);
  114.  
  115.     /*
  116.      * this bogus stuff is here simply to get c++ to shut-up about
  117.       * unreferenced parameters.
  118.      */
  119.     if( argv[argc] == "hello" )
  120.     {
  121.         printf("hello\n");
  122.     }
  123.     return(0);
  124. }
  125. #endif /* SETENVTEST */
  126.  
  127. #ifdef MALLOCHTEST
  128. #include <malloc.h>
  129. #endif
  130. #ifdef ANSIHEADERTEST
  131. #include <stdlib.h>
  132. #endif
  133. #ifdef POSIXHEADERTEST
  134. #include <unistd.h>
  135. #endif
  136.  
  137. #if defined(MALLOCHTEST) || defined(ANSIHEADERTEST) || defined(POSIXHEADERTEST)
  138. /*
  139.  * determine if certain headers are available
  140.  */
  141. #if __cplusplus || __STDC__
  142. main(int argc, char *argv[])
  143. #else
  144. main(argc,argv)
  145.     int      argc;
  146.     char    * argv[];
  147. #endif
  148. {
  149.     /*
  150.      * this bogus stuff is here simply to get c++ to shut-up about
  151.       * unreferenced parameters.
  152.      */
  153.     if( argv[argc] == "hello" )
  154.     {
  155.         printf("hello\n");
  156.     }
  157.     return(0);
  158. }
  159. #endif /* MALLOCHTEST || ANSIHEADERTEST || POSIXHEADERTEST */
  160.  
  161. #ifdef ASM_UNDTEST
  162. /*
  163.  * test requirement for underscores in external symbols
  164.  */
  165. #if __cplusplus || __STDC__
  166. #include <stdio.h>
  167. main(int argc, char *argv[])
  168. #else
  169. main(argc,argv)
  170.     int      argc;
  171.     char    * argv[];
  172. #endif
  173. {
  174.     int      myroutine();
  175.  
  176. #if i386
  177.     printf("OK\n", myroutine());
  178. #else
  179.     printf("NOT OK\n");
  180. #endif
  181.  
  182. }
  183.  
  184. #ifdef i386
  185.     asm("    .globl _myroutine");
  186.     asm("_myroutine:");
  187.     asm("    xor %eax");
  188.     asm("   ret");
  189. #endif
  190.  
  191.  
  192. #endif /* ASM_UNDTEST */
  193.  
  194.  
  195. #ifdef ASM_REPTEST
  196. /*
  197.  * test requirement for underscores in external symbols
  198.  */
  199. #if __cplusplus || __STDC__
  200. #include <stdio.h>
  201. main(int argc, char *argv[])
  202. #else
  203. main(argc,argv)
  204.     int      argc;
  205.     char    * argv[];
  206. #endif
  207. {
  208.     int      myroutine();
  209.  
  210. #if i386
  211.     printf("OK\n", myroutine());
  212. #else
  213.     printf("NOT OK\n");
  214. #endif
  215.  
  216. }
  217.  
  218. #ifdef i386
  219. #ifdef USE_UNDERSCORE
  220.     asm("    .globl _myroutine");
  221.     asm("_myroutine:");
  222. #else
  223.     asm("    .globl myroutine");
  224.     asm("myroutine:");
  225. #endif
  226.     asm("    xor %ecx");
  227.     asm("    repe");
  228.     asm("   ret");
  229. #endif
  230.  
  231.  
  232. #endif /* ASM_REPTEST */
  233.  
  234. #ifdef CONSTTEST
  235.     /*
  236.      * testing to see if the void datatype is used by this system
  237.      */
  238.     const char *
  239.     function()
  240.     {
  241.         static const char t[] = "hello";
  242.  
  243.         return(t);
  244.     }
  245. #endif
  246.  
  247. #ifdef MALLOC_COMPILETEST
  248.  
  249. #if __cplusplus
  250. DATATYPE * malloc( SIZETYPE size)
  251. #else
  252. DATATYPE *
  253. malloc( size)
  254.     SIZETYPE     size;
  255. #endif
  256. {
  257.     if( size > 0 )
  258.     {
  259.         return(0);
  260.     }
  261.     
  262.     return(0);
  263. }
  264.  
  265. #endif /* MALLOC_COMPILETEST */
  266.  
  267. #ifdef FREE_COMPILETEST
  268.  
  269. #if __cplusplus
  270. FREETYPE free( DATATYPE *data)
  271. #else
  272. FREETYPE
  273. free(data)
  274.     DATATYPE *data;
  275. #endif
  276. {
  277.     if( ! data )
  278.     {
  279.         printf("foo\n");
  280.     }
  281. }
  282.  
  283. #endif /* FREE_COMPILETEST */
  284.  
  285. #ifdef MEM_COMPILETEST
  286.  
  287. MEMDATA *memcpy __stdcargs((MEMDATA *ptr1, CONST MEMDATA *ptr2, register MEMSIZE len));
  288.  
  289. #if __cplusplus
  290. MEMDATA * memccpy(
  291.     MEMDATA        * ptr1,
  292.     CONST MEMDATA    * ptr2,
  293.     int          ch,
  294.     MEMSIZE          len )
  295. #else
  296. MEMDATA *
  297. memccpy(ptr1,ptr2,ch,len)
  298.     MEMDATA        * ptr1;
  299.     CONST MEMDATA    * ptr2;
  300.     int          ch;
  301.     MEMSIZE          len;
  302. #endif
  303. {
  304.     /*
  305.      * just make use of all the passed arguments so that we don't get bogus
  306.      * warning messages about unused arguments.  What we do doesn't have
  307.      * to make sense since we aren't going to run this.
  308.      */
  309.     if( (ptr1 == ptr2) && (ch != len) )
  310.     {
  311.         return(ptr1);
  312.     }
  313.     return(memcpy(ptr1,ptr2,len));
  314. }
  315.  
  316. #endif /* MEM_COMPILETEST */
  317.  
  318. #ifdef STR_COMPILETEST
  319.  
  320. #include <string.h>
  321. #if __cplusplus
  322. STRSIZE strlen( CONST char * str1 )
  323. #else
  324. STRSIZE
  325. strlen(str1)
  326.     CONST char * str1;
  327. #endif
  328. {
  329.     if( str1[0] != '\0')
  330.     {
  331.         return(1);
  332.     }
  333.     return(0);
  334. }
  335.  
  336. #endif /* STR_COMPILETEST */
  337.  
  338. #ifdef WRT_COMPILETEST
  339. #if __cplusplus
  340. int write(int fd, CONST char * buf, WRTSIZE size)
  341. #else
  342. int
  343. write(fd,buf,size)
  344.      int          fd;
  345.      CONST char    * buf;
  346.      WRTSIZE      size;
  347. #endif
  348. {
  349.     if( buf[fd] == (CONST char) size)
  350.     {
  351.         return(1);
  352.     }
  353.     return(0);
  354. }
  355.  
  356. #endif /* WRT_COMPILETEST */
  357.  
  358.  
  359. #ifdef PRE_DEFINES
  360.  
  361. /*
  362.  * this is used by the Configure script to get the compiler pre-defined symbol
  363.  * for this
  364.  */
  365. #if __cplusplus
  366. #include <stdio.h>
  367. main(int argc, char *argv[])
  368. #else
  369. main(argc,argv)
  370.     int      argc;
  371.     char    * argv[];
  372. #endif
  373. {
  374.     int      varcnt = 0;
  375.  
  376. #if __GNUC__
  377.     if (__GNUC__ > 1)
  378.         printf("(__GNUC__ == %d)", __GNUC__);
  379.     else
  380.         printf("__GNUC__");
  381.     varcnt++;
  382. #endif
  383. #if __STDC__ 
  384.     if( varcnt > 0 )
  385.     {
  386.         printf(" && ");
  387.     }
  388.     printf("__STDC__");
  389.     varcnt++;
  390. #endif
  391. #if __HIGHC__ 
  392.     if( varcnt > 0 )
  393.     {
  394.         printf(" && ");
  395.     }
  396.     printf("__HIGHC__");
  397.     varcnt++;
  398. #endif
  399. #if __C89__ 
  400.     if( varcnt > 0 )
  401.     {
  402.         printf(" && ");
  403.     }
  404.     printf("__C89__");
  405.     varcnt++;
  406. #endif
  407. #if __cplusplus
  408.     if( varcnt > 0 )
  409.     {
  410.         printf(" && ");
  411.     }
  412.     /*
  413.      * this bogus stuff is here simply to get c++ to shut-up about
  414.       * unreferenced parameters.
  415.      */
  416.     if( argv[argc] == "hello" )
  417.     {
  418.         printf("hello\n");
  419.     }
  420.     printf("__cplusplus");
  421.     varcnt++;
  422. #endif
  423.  
  424.     /*
  425.      * if we found no pre-defines, print out the word none, so we can tell the
  426.      * difference between compilation failures and no pre-defs.
  427.      */
  428.     if( varcnt == 0 )
  429.     {
  430.         printf("none");
  431.         varcnt++;
  432.     }
  433.  
  434.     if( varcnt > 0 )
  435.     {
  436.         printf("\n");
  437.     }
  438.     return(0);
  439. }
  440.  
  441. #endif /* PRE_DEFINES */
  442.     
  443.  
  444. #ifdef SIGIOTTEST
  445. #include <sys/types.h>
  446. #include <signal.h>
  447.     int
  448.     function()
  449.     {
  450.         int signal = SIGIOT;
  451.         return(signal);
  452.     }
  453. #endif
  454. #ifdef SIGABRTTEST
  455. #include <sys/types.h>
  456. #include <signal.h>
  457.     int
  458.     function()
  459.     {
  460.         int signal = SIGABRT;
  461.         return(signal);
  462.     }
  463. #endif
  464.  
  465. #ifdef CHANGESTR
  466.  
  467. #include <stdio.h>
  468.  
  469. #define FILEBUFSIZE    (50*1024)
  470.  
  471. char iobuffer[FILEBUFSIZE];
  472.  
  473. main(argc,argv)
  474.     int          argc;
  475.     char        * argv[];
  476. {
  477.     unsigned int      cnt;
  478.     FILE        * fp;
  479.     unsigned int      i;
  480.     int          len;
  481.     char        * src;
  482.     char        * tgt;
  483.  
  484.     if( argc != 4 )
  485.     {
  486.         fprintf(stderr,"Usage: changestr file oldstr newstr\n");
  487.         exit(1);
  488.     }
  489.     src = argv[2];
  490.     tgt = argv[3];
  491.  
  492.     /*
  493.      * get length of strings (note that we don't ensure that both strings
  494.      * are the same length and
  495.      */
  496.     len = strlen(src);
  497.     i   = strlen(tgt);
  498.     if( i > len )
  499.     {
  500.         fprintf(stderr,"Error: second string cannot be longer %s\n",
  501.                 "than first string");
  502.         exit(2);
  503.     }
  504.  
  505.     fp = fopen(argv[1],"r+");
  506.  
  507.     if( fp == NULL )
  508.     {
  509.         fprintf(stderr,"Can't open %s\n",argv[1]);
  510.         exit(3);
  511.     }
  512.  
  513.     /*
  514.      * read the entire file in (note that if the file is bigger
  515.      * than the specified blocksize, we won't be able to
  516.      * process it.
  517.      */
  518.  
  519.     cnt = fread(iobuffer,1,FILEBUFSIZE,fp);
  520.     if( cnt <= 0 )
  521.     {
  522.         fprintf(stderr,"Read error when reading %s\n",argv[1]);
  523.         exit(4);
  524.     }
  525.  
  526.     for(i=0; i < (cnt-len); i++)
  527.     {
  528.         if( memcmp(iobuffer+i,src,len) == 0 )
  529.         {
  530.             memcpy(iobuffer+i,tgt,len);
  531.             i += len-1;
  532.         }
  533.     }
  534.  
  535.     if( fseek(fp,0L,0) != 0 )
  536.     {
  537.         fprintf(stderr,"Failed to seek to correct location\n");
  538.         exit(5);
  539.     }
  540.  
  541.     if( fwrite(iobuffer,1,cnt,fp) != cnt )
  542.     {
  543.         fprintf(stderr,"Failed to write new data\n");
  544.         exit(6);
  545.     }
  546.  
  547.     fclose(fp);
  548.     
  549.     exit(0);
  550.  
  551.  
  552. #endif /* CHNAGESTR */
  553.  
  554.  
  555. #ifdef TESTDATAMC
  556.  
  557. #include <stdio.h>
  558.  
  559. main(argc,argv)
  560.     int          argc;
  561.     char        * argv[];
  562. {
  563.     char          buffer[30];
  564.  
  565.     buffer[0] = '\0';
  566.     buffer[1] = '\0';
  567.     buffer[2] = '\0';
  568.     buffer[3] = '\0';
  569.     buffer[4] = '\0';
  570.     buffer[5] = '\0';
  571.     buffer[6] = '\0';
  572.     buffer[7] = '\0';
  573.     buffer[8] = '\0';
  574.  
  575.     DataMC(buffer, "   y",5);
  576.     DataMC(buffer+4, "yy",3);
  577.  
  578.     DataMC(buffer+1, buffer,7);
  579.     DataMC(buffer,   "x",1);
  580.  
  581.     printf("%s\n",buffer);
  582.  
  583.     return(0);
  584. }
  585.  
  586. /*
  587.  * we need to have our own memcpy here in order to find out if there will be
  588.  * some problem where the kludged version of memcpy (now should be named 
  589.  * DataMC) at least one system (SGI) has gotten into an infinite loop
  590.  * when the modified DataMC ended up calling the library's memcpy
  591.  */
  592. memcpy()
  593. {
  594.     write(1,"Infinite loop\n",14);
  595.     exit(1);
  596. }
  597.  
  598. #endif /* TESTDATAMC */
  599.  
  600. #ifdef TESTDATAMS
  601. #include <stdio.h>
  602.  
  603. main(argc,argv)
  604.     int          argc;
  605.     char        * argv[];
  606. {
  607.     char          buffer[30];
  608.  
  609.     buffer[0] = '\0';
  610.     buffer[1] = '\0';
  611.     buffer[2] = '\0';
  612.     buffer[3] = '\0';
  613.     buffer[4] = '\0';
  614.     buffer[5] = '\0';
  615.     buffer[6] = '\0';
  616.     buffer[7] = '\0';
  617.     buffer[8] = '\0';
  618.     buffer[9] = '\0';
  619.     buffer[10] = '\0';
  620.  
  621.     DataMS(buffer,  'x',1);
  622.     DataMS(buffer+1,' ',3);
  623.     DataMS(buffer+4,'y',3);
  624.  
  625.     printf("%s\n",buffer);
  626. }
  627.  
  628. memset()
  629. {
  630.     write(1,"Infinite loop\n",14);
  631.     exit(1);
  632. }
  633.  
  634. #endif /* TESTDATAMS */
  635.  
  636. #ifdef COMPARETEST 
  637.  
  638. #include <stdio.h>
  639. #include <string.h>
  640.  
  641. #if __cplusplus
  642. #include <stdlib.h>
  643. main(int argc, char *argv[])
  644. #else
  645. main(argc,argv)
  646.     int      argc;
  647.     char    * argv[];
  648. #endif
  649. {
  650.     char          buffer[10];
  651.     char          buf2[10];
  652.     int          result;
  653.  
  654.     buffer[0] = 'a';
  655.     buffer[1] = '\0';
  656.     buf2[0]   = 0xff;
  657.     buf2[1] = '\0';
  658.  
  659.     /*
  660.      * just to get c++ and some ANSI C compilers to shutup.  argc will
  661.      * be more than 1 when running this test.
  662.      */
  663.     if( argc > 10 )
  664.     {
  665.         result = strcmp(argv[0],"junkstr");
  666.     }
  667.     else
  668.     {
  669.         result = COMPARETEST (buffer,buf2,1);
  670.     }
  671.  
  672. #ifdef TESTCHAR
  673.     result = -result;
  674. #endif
  675.  
  676.     if( result < 0 )
  677.     {
  678.         exit(0);
  679.     }
  680.     else
  681.     {
  682.         exit(1);
  683.     }
  684.  
  685. }
  686.     
  687.  
  688. #endif /* COMPARETEST */
  689.